﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

#region Usings

using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;

#endregion

namespace ScrumTable.BL.DM.DataManagement
{
    /// <summary>
    /// Represents the an event arguments class which contains additional
    /// information about an event if something has changed inside a
    /// domain object.
    /// </summary>
    public class MultipleDomainChangedEventArgs : DomainChangedEventArgs
    {
        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------

        private readonly IList<DomainChangedEventArgs> _children = new List<DomainChangedEventArgs>();

        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------

        /// <summary>
        /// Gets the child event argument instances.
        /// </summary>
        public IEnumerable<DomainChangedEventArgs> Children
        {
            get { return _children; }
        }

        /// <summary>
        /// Gets the number of child event listed in the current MultipleDomainChangedEventArgs instance.
        /// </summary>
        public int EventCount
        {
            get { return _children.Count; }
        }

        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------

        /// <summary>
        /// Creates a new MultipleDomainChangedEventArgs instance. This constructor
        /// is protected internal because its needed by derived or package classes only.
        /// </summary>
        internal protected MultipleDomainChangedEventArgs()
            : base(string.Empty)
        {
        }

        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        /// <summary>
        /// Returns true if a child element contains an event which changed
        /// the given property.
        /// </summary>
        /// <param name="propertyName">Specifies the name of the property to check.</param>
        public override bool ContainsPropertyName(string propertyName)
        {
            return (Children.FirstOrDefault(prop => prop.IsValidChangedElement && prop.PropertyName == propertyName) != null);
        }


        /// <summary>
        /// Returns true if a the currrent element contains an event which changed
        /// the given property.
        /// </summary>
        /// <param name="propertyId">Specifies the id of the property to check.</param>
        /// <param name="checkParentId">Specifies if the parent id should contains the id.</param>
        public override bool ContainsElementId(string propertyId, bool checkParentId=false)
        {
            return (Children.FirstOrDefault(prop => prop.IsValidChangedElement && (checkParentId ? prop.ChangedElement.Parent.Id == propertyId : prop.ChangedElement.Id == propertyId)) != null);
        }

        /// <summary>
        /// Returns true if a the currrent element contains an event with the given type
        /// </summary>
        public override bool ContainsType<T>(bool checkParentType = false) 
        {
            return (Children.FirstOrDefault(prop => prop.IsValidChangedElement && (checkParentType ? prop.ChangedElement.Parent.GetType() == typeof(T) : prop.ChangedElement.GetType() == typeof(T))) != null);
        }

        /// <summary>
        /// Adds a child event to the current event argument.
        /// </summary>
        /// <param name="toAdd">Specifies the child event to add.</param>
        internal void Add(DomainChangedEventArgs toAdd)
        {
            if (toAdd != null)
            {
                _children.Add(toAdd);
            }
        }

        #endregion

        #region Events
        //--------------------------------------------------------------------
        // Events
        //--------------------------------------------------------------------

        #endregion
    }
}